package com.ljq.software_springboot.softwareprogram.controller;



import com.ljq.software_springboot.softwareprogram.entity.GroupMember;
import com.ljq.software_springboot.softwareprogram.entity.UserGroup;
import com.ljq.software_springboot.softwareprogram.imp.UserGroupServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-28
 */
@RestController
@RequestMapping("/usergroup")
@CrossOrigin(origins = "http://localhost:5173")
public class UserGroupController {
    @Autowired
    UserGroupServiceImpl groupService = new UserGroupServiceImpl();
    @PostMapping("create-group")
    public String creategroup(@RequestBody UserGroup groupRequest) {
        return  groupService.createGroup(groupRequest);
    }

    @PostMapping("/friend-groups")
    public ResponseEntity<Map<String, Object>> getFriendGroups(@RequestBody UserGroup creatorId) {
        Map<String, Object> response = new HashMap<>();
        System.out.println(creatorId.getCreatorId());
        try {
            List<UserGroup> groups = groupService.loadFriendGroups(creatorId);
            response.put("success", true);
            response.put("groups", groups);
            System.out.println(groups.size());
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取分组信息失败！");
        }
        return ResponseEntity.ok(response);
    }
}
